package com.example.fist.control;

import com.example.fist.dao.CodeDao;
import com.example.fist.dao.UserDao;
import com.example.fist.entity.AdminEntity;
import com.example.fist.entity.CodeEntity;
import com.example.fist.personRepository.AdminJpaRepository;
import com.example.fist.personRepository.CodeJpaRepository;
import com.example.fist.util.Json.ErrorJson;
import com.example.fist.util.mail.Mailbox;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.mail.MessagingException;
import javax.persistence.EntityManager;
import java.util.Date;
import java.util.UUID;


@RestController
@CrossOrigin(allowCredentials = "true")
public class Pass {

    @Autowired
    private AdminJpaRepository userRepository;

    @Autowired
    private CodeJpaRepository codeRepository;

    @Autowired
    private EntityManager entityManager;
    /**
     * 发送邮件
     * @param name
     * @return
     */
    @RequestMapping("/pass/senCode")
    public String  sendCode(String name){
        if(name==null){
            return  new  ErrorJson(20001,"用户名参数未设置").toString();
        }
        UserDao dao=new UserDao(userRepository);
        AdminEntity admin=dao.getAdmin(name);
        if(admin==null){
            return  new ErrorJson(20002,"用户名不存在").toString();
        }
        String  code=  UUID.randomUUID().toString();
        CodeEntity codeEntity=new CodeEntity();
        codeEntity.setCode(code);
        codeEntity.setAdId(admin.getAdId());
        codeEntity.setCreateTime(new Date());

        CodeDao coddao=new CodeDao(codeRepository,entityManager);
        int i= coddao.save(codeEntity);

        String text="欢迎您："+name+"\n欢迎使用及第重置密码，请点击下方链接进行重置密码\nhttp:\\\\127.0.0.1?id="+admin.getAdId()+"&code="+code;
        Mailbox ma= null;
        try {
            ma = new Mailbox(admin.getMail(),text,"及第","密码重置");
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        if(! ma.sendMessage()){
                return  new ErrorJson(2003,"邮件发送失败").toString();
        }
        JSONObject js=new JSONObject();
        try {
            js.put("messg","Ok");
            js.put("code",200);
        } catch (Exception e){
        }
        return js.toString();
    }
    @RequestMapping("/pass/resetpass")
    public  String ResetPass(String code,String newpass,int id) {
        System.out.printf(code);
        CodeDao coddao = new CodeDao(codeRepository,entityManager);
        CodeEntity codeEntity = coddao.getCodeId(id,code);
        if (codeEntity.getIsvisible() == 0 && code.equals(codeEntity.getCode())) {
            return new ErrorJson(20003, "该链接已失效").toString();
        }
        UserDao userDao = new UserDao(userRepository);
        int i = userDao.ChangPass(newpass, codeEntity.getAdId());
        if (i > 0) {
            codeEntity.setIsvisible(0);
            codeEntity.setUseTime(new Date());
            coddao.update(codeEntity);
            return new ErrorJson(200, "OK").toString();
        } else {
            return new ErrorJson(2000, "密码修改失败").toString();
        }
    }
}
